define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam.grade_detail/detail' + location.search + '&ids='+$('#ids').val(),
                    add_url: 'exam.grade_detail/add',
                    edit_url: 'exam.grade_detail/edit',
                    del_url: 'exam.grade_detail/del',
                    multi_url: 'exam.grade_detail/multi',
                    import_url: 'exam.grade_detail/import',
                    table: 'exam_grade_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('题目ID')},
                        {field: 'question.kind_text', title: __('题目类型')},
                        {field: 'question.title', title: __('题目'),
                            formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            // 固定列最大宽度，超出隐藏
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'question.answer', title: __('正确答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {
                                var answer = '-';
                                if (row.question.kind == 'SINGLE') {

                                    try {
                                        answer = value;
                                    } catch (e) {
                                        return '-';
                                    }
                                }

                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;'>" + answer + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'content', title: __('用户答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {

                                var answer = '-';
                                if (row.question.kind == 'SINGLE') {

                                    try {
                                        answer = value;
                                    } catch (e) {
                                        return '-';
                                    }
                                }else if(row.question.kind == 'WORD') {
                                    
                                    
                                    try {
                                        if(value.accuracy_score){
                                            answer = '';
                                            answer += '准确度评分：<b style="color:#f75444;">'+value.accuracy_score+'</b><br/>';
                                            answer += '流畅度评分：<b style="color:#f75444;">'+value.fluency_score+'</b><br/>';
                                            answer += '标准度分：<b style="color:#f75444;">'+value.standard_score+'</b><br/>';
                                            answer += '篇章总分：<b style="color:#f75444;">'+value.total_score+'</b><br/>';
                                            answer += '标红次数：<b style="color:#f75444;">'+value.markedRedNum+'</b><br/>';
                                        }else{
                                            word_answers = value.answer
                                    
                                            answer = '';
                                            for (let i = 0; i < word_answers.length; i++) {
                                                if(word_answers[i].total_score){
                                                    answer += '单词：' + word_answers[i].content + '（<b style="color:red">' + word_answers[i].total_score + '</b>）<br>';
                                                }
                                            }
                                        }
                                        

                                    } catch (e) {
                                        return '-';
                                    }
                                }else{

                                }

                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;'>" + answer + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'content', title: __('用户语音'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {
                                
                                var answer = '-';


                                try {
                                    answer = '';
                                    answer += '<audio src="'+value.singleRecord+'" controls>用户语音</audio> <br>';

                                } catch (e) {
                                    return '-';
                                }


                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;'>" + answer + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
